<?php
/**
 * Controle de Servico
 * @filesource  06/04/2011
 * @author      Estrada Virtual <http://estradavirtual.com.br>
 * @copyright   Copyright <2011> Estrada Virtual
 * @package     <<application>>
 * @subpackage  <<application>>.application.controllers
 * @version     $Id$
 */
class servico_ServicoController extends App_Controller_Crud
{
    protected $_aCamposListagem = array('fk_cliente', 'fk_tipo_servico');

    public function getSqlGrid($aCampos)
    {
        $oModelo = $this->getModelo();
        $aTableInfo = $oModelo->info();
        $tableName  = $aTableInfo['name'];

        $oDb = Zend_Db_Table_Abstract::getDefaultAdapter();

        $sql = $oDb->select()->from($tableName, array('id_servico', 'st_nome', 'vl_valor'));
        $sql->join(array('c' => 'cliente'), 'c.id_cliente = fk_cliente', array('Cliente' => 'st_nome'))
            ->join(array('ts' => 'tipo_servico'), 'ts.id_tipo_servico = fk_tipo_servico', array('Tipo' => 'st_nome'));

        return $sql;
    }

    public function extrasGrid($oGrid)
    {
        if (!$this->_getParam('_exportTo')) {
            $baseUrl   = $this->view->baseUrl();
            $sControle =  'servico/servico';
            $sId       = 'id_servico';
            $extra = new Bvb_Grid_Extra_Column();
            $extra->position ('right')
                  ->name('Ação')
                  ->decorator ('
                            <a class="ui-icon ui-icon-pencil" href="' . $baseUrl . '/'. $sControle . '/visualizar/id/{{' . $sId . '}}/cs_situacao/A"><span>visualizar</span></a>
                            <a class="ui-icon ui-icon-pencil" href="' . $baseUrl . '/'. $sControle . '/formulario/id/{{' . $sId . '}}"><span>alterar</span></a>
                            <a class="ui-icon ui-icon-close" href="' . $baseUrl . '/'. $sControle . '/excluir/id/{{' . $sId . '}}"><span>excluir</span></a>
                        ');
            $oGrid->addExtraColumns($extra);
        }
        return $oGrid;
    }

    public function visualizarAction()
    {
        $servico = new Model_Servico();

        if (!$id = (int) $this->_request->getParam('id')) {
            $this->_addMessage('servico/servico', 'Serviço inválido');
        }

        $rowServico = $servico->find($id)->current();

        $this->view->assign('row', $rowServico);
        $this->view->assign('rowTipoServico', $rowServico->findParentModel_TipoServico());
        $this->view->assign('rowCliente', $rowServico->findParentModel_Cliente());
        $this->view->assign('rowSetTarefas', $rowServico->findModel_Tarefa());
        $this->view->assign('rowSetAnotacoes', $rowServico->findModel_Anotacao());
        $this->view->assign('rowSetArtefatos', $rowServico->findModel_Artefato());
    }

    public function novoAction()
    {
        $fk_cliente = $this->_getParam('fk_cliente');

        if ($this->getRequest()->isPost()) {
            $modelServico = new Model_Servico();
            $rowServico = $modelServico->createRow($this->_getAllParams());
            $idServico = $modelServico->gravar($rowServico->toArray());
            $this->_addMessage('Serviço criado com sucesso', 'servico/servico/visualizar/id/' . $idServico);
        }

        $modelCliente = new Model_Cliente();
        $rowCliente = $modelCliente->find($fk_cliente)->current();
        $this->view->rowCliente = $rowCliente;
        $this->_carregarExtrasFormulario();
    }
}
